package org.lsms.servlet;

/**
 * servlet used to process paper assignment(from member to reviewer,
 * more than one paper)
 * @author ZhanXueliang
 * 2010.8.31
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;
import org.lsms.mail.MailSenderInfo;
import org.lsms.mail.SimpleMailSender;

public class paper_assign2_all extends HttpServlet {

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			int member_id = Integer.parseInt(request.getParameter("member_id"));
			int paper_id = Integer.parseInt(request.getParameter("paper_id"));
			String ids = request.getParameter("reviewer_id");
			//System.out.println(paper_id+","+member_id);
			//System.out.println(ids);
			String [] reviewer_ids = ids.split(",");
			int reviewer_num = reviewer_ids.length;
			
			DaoImpl impl = new DaoImpl();
			
			
			for(int i=0;i<reviewer_num;i++){
				int reviewer_id = Integer.parseInt(reviewer_ids[i]);
//				System.out.println(reviewer_id);
				
				User user = impl.get_user_info(reviewer_id);
				String loginname = user.getLoginname();
				String firstname = user.getFirstname();
				String middlename = user.getMiddlename();
				String lastname = user.getLastname();
				String email = user.getEmail();
//				System.out.println(email);
				
				MailSenderInfo mailInfo = new MailSenderInfo();
				mailInfo.init(mailInfo);
				mailInfo.setSubject("LSMS & ICSEE 2010 Paper Assign");
				
				mailInfo.setToAddress(email);
				String content = "Dear Mr/Mrs " + firstname+" "+middlename+" "+lastname+'\n'+'\n';
				String letter_content = impl.get_mail_content(4);
				content +=letter_content;
				String ps = "\n\nPS:\nYour Loginname: "+loginname+".\nif you have forgotten your password, " +
						"you can click the \"forget password?\" button to reset it.";
				content +=ps;
				mailInfo.setContent(content);
				
				SimpleMailSender sms = new SimpleMailSender();
				if(impl.paper_assign2(member_id, reviewer_id, paper_id) && sms.sendTextMail(mailInfo)){
					continue;
				}else{
					String member_info = "Problem occurs when assigning paper " +
							"to reviewer "+reviewer_id+"! Please try again later!";
					response.sendRedirect("member/member_show_info.jsp?member_info="+member_info);
				}
			}
			String member_info = "Paper "+paper_id+" has been assigned to reviewer " +ids;
			response.sendRedirect("member/member_show_info.jsp?member_info="+member_info);
			
	}

	public void init() throws ServletException {
		
	}

}
